package com.efast.cafe.commponent.job.bean;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

import org.springframework.data.redis.core.StringRedisTemplate;

import com.efast.cafe.framework.spring.SpringContextUtils;
import com.efast.cafe.util.ConfigUtil;

public class ScheduleExecuteLog implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = -1675835105864104357L;

	private static StringRedisTemplate redisTemplate;
	
	private  String group;
	
	private  String logKey;
	
	public static ScheduleExecuteLog getInstance(String jobName){
		if(redisTemplate==null){
			redisTemplate = SpringContextUtils.getBean(StringRedisTemplate.class);
		}
		ScheduleExecuteLog log = new ScheduleExecuteLog();
		log.group = ConfigUtil.getValue("spring.application.name");
		log.logKey = "quartz:logs:"+log.group+":"+jobName;
		return log;
	}
	
	public void putLog(String logMsg){
		String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"));
		redisTemplate.opsForList().rightPush(logKey, time+"=>"+logMsg);
	}
	
	public List<String> getLogs(){
		return redisTemplate.opsForList().range(logKey, 0, -1);
	}
	
	public  void clearLogs(){
		redisTemplate.delete(logKey);
	}
}
